package cn.milk.dao;

import cn.milk.model.Task;
import cn.milk.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TaskDAO {
    public List<Task> getTasks(String search, int page, int pageSize) throws SQLException {
        List<Task> tasks = new ArrayList<>();
        String sql = "SELECT * FROM task WHERE menuname LIKE ? LIMIT ?, ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, "%" + search + "%");
            stmt.setInt(2, (page - 1) * pageSize);
            stmt.setInt(3, pageSize);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Task task = new Task();
                task.setId(rs.getInt("id"));
                task.setOrdersid(rs.getInt("ordersid"));
                task.setMenuname(rs.getString("menuname"));
                task.setQuantity(rs.getInt("quantity"));
                task.setStatus(rs.getInt("status"));
                task.setCretime(rs.getString("cretime"));
                tasks.add(task);
            }
        }
        return tasks;
    }

    public int getTotalCount(String search) throws SQLException {
        String sql = "SELECT COUNT(*) FROM task WHERE menuname LIKE ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, "%" + search + "%");
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return rs.getInt(1);
            }
        }
        return 0;
    }

    public void addTask(Task task) throws SQLException {
        String sql = "INSERT INTO task (ordersid, menuname, quantity, status) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, task.getOrdersid());
            stmt.setString(2, task.getMenuname());
            stmt.setInt(3, task.getQuantity());
            stmt.setInt(4, task.getStatus());
            stmt.executeUpdate();
        }
    }

    public void updateTask(Task task) throws SQLException {
        String sql = "UPDATE task SET ordersid = ?, menuname = ?, quantity = ?, status = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, task.getOrdersid());
            stmt.setString(2, task.getMenuname());
            stmt.setInt(3, task.getQuantity());
            stmt.setInt(4, task.getStatus());
            stmt.setInt(5, task.getId());
            stmt.executeUpdate();
        }
    }

    public void deleteTask(int id) throws SQLException {
        String sql = "DELETE FROM task WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        }
    }

    public Task getTaskById(int id) throws SQLException {
        String sql = "SELECT * FROM task WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                Task task = new Task();
                task.setId(rs.getInt("id"));
                task.setOrdersid(rs.getInt("ordersid"));
                task.setMenuname(rs.getString("menuname"));
                task.setQuantity(rs.getInt("quantity"));
                task.setStatus(rs.getInt("status"));
                task.setCretime(rs.getString("cretime"));
                return task;
            }
        }
        return null;
    }
}
